using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


using FarseerGames.FarseerXNAPhysics.Mathematics;

namespace Traffic.Path
{
    public class StateNode : AStarNode
    {        
        private Vector2 mPosition;
        public Vector2 Position
        {
            get { return mPosition; }
            set { mPosition = value; }
        }

        private Vector2 mVelocity;
        public Vector2 Velocity
        {
            get { return mVelocity; }
            set { mVelocity = value; }
        }        
        
        public StateNode(StateNode AParent, AStarNode AGoalNode, float ACost, Vector2 p)
            : base(AParent, AGoalNode, ACost)
        {            
            this.mPosition = p;
        }
               
        private void AddSuccessor(ref ArrayList ASuccessors, Vector2 move)
        {
            float currentCost = 1f;
            
            StateNode NewNode = new StateNode(this, GoalNode, Cost + currentCost, Position + move);

            ASuccessors.Add(NewNode);
        }

        /// <summary>
        /// Calculates is the given state is close enough to be the same state as this
        /// </summary>
        /// <param name="ANode"></param>
        /// <returns></returns>
        public override bool IsSameState(AStarNode node)
        {
            if (node == null)
            {
                return false;
            }

            float dist = Calculator.DistanceBetweenPointAndPoint(((StateNode)node).Position, Position);

            if (dist < 1f)
            {
                return true;
            }
            return false;           
        }

        /// <summary>
        /// Calculates the estimated cost for the remaining trip to the goal.
        /// </summary>
        public override void Calculate()
        {
            if (GoalNode != null)
            {
                GoalEstimate = Calculator.DistanceBetweenPointAndPoint(((StateNode)GoalNode).Position, Position);          
            }
            else
            {
                GoalEstimate = 0f;
            }
        }

        /// <summary>
        /// Gets all successors nodes from the current node and adds them to the successor list
        /// </summary>
        /// <param name="ASuccessors">List in which the successors will be added</param>
        public override void GetSuccessors(ref ArrayList ASuccessors)
        {
            ASuccessors.Clear();

            AddSuccessor(ref ASuccessors, new Vector2(10, 0));
            AddSuccessor(ref ASuccessors, new Vector2(0, 10));
            AddSuccessor(ref ASuccessors, new Vector2(-10, 0));
            AddSuccessor(ref ASuccessors, new Vector2(0, -10));            
        }

        /// <summary>
        /// Prints information about the current node
        /// </summary>
        public override void PrintNodeInfo()
        {
            //Console.WriteLine("X:\t{0}\tY:\t{1}\tCost:\t{2}\tEst:\t{3}\tTotal:\t{4}", FX, FY, Cost, GoalEstimate, TotalCost);
        }

    
    }
}
